//
//  DDInterviewVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 2016/10/25.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class DDInterviewVC: LDBaseTableViewVC {

    
    
    var model:DDInterviewListDetailModel?
    
    private var descList:[String]?
    
    private let cellID = "cellid"
    private let cellTypeID = "cellTypeID"
    private let cellDescID = "cellDescID"
    
    var cellTitleArr = ["面试类型","面试时间","联系方式","面试地点","其它说明"]
    var type:DDWaitInterviewType? {
        
        didSet{
            
            if type == .Video {
                
                self.cellTitleArr = ["面试类型","面试时间","其它说明"]
            }
        }
    }
    
    var isWaitInterView:Bool?
    var detailID:String?
    
    lazy var headerV:DDinterviewHeaderView = {
        return DDinterviewHeaderView(frame:CGRectMake(0, 0, LDFrame.ScreenWidth, 85)) }()
    
    override func viewDidLoad() {
        
        tableView.backgroundColor = LDGolbalColor.BgGoundGray
        tableView.tableHeaderView = headerV
        tableView.separatorStyle = .None
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        requsetNet()
    }
    
    
    func requsetNet() {
        
        if detailID == nil { return }
        LDUserInfoNet.sharedInstance.getInterviewInfoListDetail(detailID!, succ: { [weak self] (dict) in
            
            self?.model = DDInterviewListDetailModel.yy_modelWithDictionary(dict) ?? DDInterviewListDetailModel()
            
            let iVModel = self?.model?.interview
            self?.descList = [iVModel!.interviewdt ?? "",iVModel!.link_mobile ?? "",iVModel!.address ?? "",iVModel!.remark ?? "无"]
            self?.headerV.nameTitle = iVModel?.name
            self?.tableView.reloadData()
            self?.setBottomBtn()
            
        }) { (err) in
            
            debugPrint(err)
        }
    }
    
    
    func getCellH() -> CGFloat{
    
        let gh = String.getHeightFormAttr(8, title: "", size: CGSizeMake(LDFrame.ScreenWidth - 130, 1000), font: LDFont.Font14)
        return gh + 40
    }
    
    func setBottomBtn() {
        
        if isWaitInterView != true { return }
        
        let btnV = LDBottomBtnView.init(frame: CGRectMake(0, 0, LDFrame.ScreenWidth, 80))
        btnV.title = "取 消 邀 约"
        btnV.actionBtn?.backgroundColor = LDFontColor.Red.colorWithAlphaComponent(0.9)
        tableView.tableFooterView = btnV
        
        
        btnV.actionBtn?.rac_signalForControlEvents(.TouchUpInside).subscribeNext({ [weak self] (obj) in
            
            let newModel = self!.model!.interview!
            newModel.state = "2"
            
            if DDClientInfoManager.clientType() == DDClientType.Cattle {
                
                newModel.jobfinder_id = DDLoginUserManager.userID()
            } else {
                
                newModel.emplyoer_id = DDLoginUserManager.userID()
            }
            
            let dict = newModel.yy_modelToJSONObject() as?[String:String] ??  [String:String]()
            
            LDUserInfoNet.sharedInstance.editAndSaveInterviewInfoDetail(dict, succ: { (id) in
                
                LDShowHUB.shareInstance.showSucc("取消邀约成功")
                self?.navigationController?.popViewControllerAnimated(true)
            }, fail: { (err) in
                
            })
        })
        
    }
    
}
extension DDInterviewVC {
    
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        if model?.interview != nil && model?.job != nil{
            
            return 2
        }
        
        return 0
    }
    // MARK: - Table view data source
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        switch section {
        case 0:
            
            return 1
        case 1:
            
            return cellTitleArr.count
        default:
            
            return 0
        }
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        
        return creatCell(tableView,index: indexPath)
        
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        

        
        if indexPath.section == 0 {
            
            return 180
        } else {
            
            switch indexPath.row {
            case 0:
                
                return 80
            default:
                
                return getCellH()
            }
        }

    }
    
    
    func creatCell(tableView: UITableView,index:NSIndexPath) ->(UITableViewCell) {
        
        if index.section == 0 {
            
            var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as?RecommendJobTableCell
            
            if cell == nil {
                cell = RecommendJobTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellID)
            }
            cell?.ddModel = model?.job ?? DDInterviewListDetailJobModel()

            return cell!
            
        } else {
            
            if index.row == 0 {
                
                var cell = tableView.dequeueReusableCellWithIdentifier(cellTypeID) as? DDWaitInterviewTypeCellVideoCell
                
                if cell == nil {
                    
                    cell = DDWaitInterviewTypeCellVideoCell.init(type: .TopGamp, margin: 5, style: .Default, reuseIdentifier: cellTypeID)
                }
                
                if model?.interview.type == "1" {
                    cell?.interViewType = .Present
                } else {
                    cell?.interViewType = .Video
                }
                
                cell?.titleLab.text = cellTitleArr[index.row]
                
                return cell!
                
            } else {
                
                var cell = tableView.dequeueReusableCellWithIdentifier(cellDescID) as? DDWaitInterviewTypeCellVideoDescCell
                
                if cell == nil {
                    cell = DDWaitInterviewTypeCellVideoDescCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellDescID)
                }
                
                cell?.titleLab.text = cellTitleArr[index.row]
                cell?.descTextView.attributedText = String.getAttrFormString(lineH: 8, text: descList![index.row - 1], font: LDFont.Font14)
                
                return cell!
            }
            
        }
    }
    
}